package algorithms.tree;

/**
 * 二叉树抽象
 * 
 * @author shaoyuxia
 *
 * @param <K>
 */
public abstract class BinaryTree<K extends Comparable<K>> extends Tree<K, BinaryNode<K>> {

	// 节点数
	private Integer size = 0;

	public BinaryTree(K key) {
		this.rootNode = new BinaryNode<K>(key);
		size = 1;
	}

	public BinaryTree() {

	}

	public Integer getDepth() {
		if (rootNode != null)
			return rootNode.getDepth();
		return 0;
	}

	public void insertNode(BinaryNode<K> node) {
		if (rootNode == null) {
			rootNode = node;
		} else {
			insert(rootNode, node);
			size++;
		}
	}

	/**
	 * 删除节点
	 * 
	 * @param key
	 */
	public void deleteNodeByKey(K key) {
		throw new RuntimeException("not support");
	}

	/**
	 * 指定节点进行左旋
	 * 
	 * 左旋右上位，旧右为新左，新新左为旧
	 * 
	 * @param key
	 */
	public void rotateLeft(K key) {
		throw new RuntimeException("not support");
	}

	/**
	 * 指定节点右旋
	 * 
	 * @param key
	 */
	public void rotateRight(K key) {
		throw new RuntimeException("not support");
	}

	public Node<K> getNodeByKey(K key) {
		throw new RuntimeException("not support");
	}

	protected void insert(BinaryNode<K> baseNode, BinaryNode<K> node) {
		throw new RuntimeException("not support");
	}

	public Integer getSize() {
		return size;
	}

	public void setSize(Integer size) {
		this.size = size;
	}

	@Override
	public String preorderTraversing() {
		return rootNode.preorderTraversing();
	}

	@Override
	public String inorderTraversing() {
		return rootNode.inorderTraversing();
	}

	@Override
	public String postorderTraversing() {
		return rootNode.postorderTraversing();
	}

	@Override
	public String toString() {
		return preorderTraversing();
	}
}
